package com.situ.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Banji;
import com.situ.web.entity.Course;
import com.situ.web.entity.PageBean2;
import com.situ.web.service.IBanjiService;
import com.situ.web.service.impl.BanjiServiceImpl;

import net.sf.json.JSONArray;

@WebServlet("/banji")
public class BanjiServlet extends HttpServlet{
	private IBanjiService banjiService = new BanjiServiceImpl(); 
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//解决乱码问题
		resp.setContentType("text/html;charset=utf-8");
		req.setCharacterEncoding("utf-8");
		String method = req.getParameter("method");
		if (method == null || "".equals(method)) {
			method = "pageList";
		}
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "pageList":
			pageList(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "selectById":
			selectById(req, resp);
			break;
		case "selectById2":
			selectById2(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "search":
			search(req, resp);
			break;
		case "selectBanjiCourseInfo":
			selectBanjiCourseInfo(req, resp);
			break;
		case "selectCourse":
			selectCourse(req, resp);
			break;
		default:
			break;
		}
	}

	private void selectCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		ArrayList<Course> list = banjiService.selectCourse();
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.getWriter().write(jsonArray.toString());
	}

	private void selectBanjiCourseInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Map<String, Object>> list = banjiService.selectBanjiCourseInfo();
		req.setAttribute("list", list);
		req.getRequestDispatcher("banji_list.jsp").forward(req, resp);
	}

	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// http://localhost:8080/JavaWeb1907/banji?method=pageList&pageNo=2&pageSize=3
				String pageNo = req.getParameter("pageNo");
				String pageSize = req.getParameter("pageSize");
				
				if (null == pageNo || "".equals(pageNo)) {
					pageNo = "1";
				}
				if (null == pageSize || "".equals(pageSize)) {
					pageSize = "3";
				}
				
				PageBean2 pageBean = banjiService.pageList(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
				
				req.setAttribute("pageBean", pageBean);
				req.getRequestDispatcher("banji_page_list.jsp").forward(req, resp);
	}

	private void selectById2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String id = req.getParameter("id");
		Banji banji = banjiService.selectById2(Integer.parseInt(id));
		// 放到一块内存区域里面
		req.setAttribute("banji", banji);
		// 请求转发到student_update.jsp页面进行展示
		req.getRequestDispatcher("/banji_update.jsp").forward(req, resp);
	}

	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String searchName = req.getParameter("searchName");
		ArrayList<Banji> list = (ArrayList<Banji>) banjiService.search(searchName);
		// 放到一块内存区域里面
		req.setAttribute("list", list);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/banji_list.jsp").forward(req, resp);
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ArrayList<Banji> list = (ArrayList<Banji>) banjiService.selectAll();

		// 放到一块内存区域里面
		req.setAttribute("list", list);
		// 请求转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/banji_list.jsp").forward(req, resp);

	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		Banji banji = new Banji(name);
		boolean isSuccess = banjiService.insert(banji);
		// 重定向到/JavaWeb1907/banji
		resp.sendRedirect("/JavaWeb1907/banji");
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		boolean isSuccess = banjiService.deleteById(Integer.parseInt(id));
		// 删除完了之后重定向到列表界面再展示所有数据
		resp.sendRedirect(req.getContextPath() + "/banji?method=selectAll");
	}

	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

		ArrayList<Banji> list = (ArrayList<Banji>) banjiService.selectById();
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.getWriter().write(jsonArray.toString());
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		Banji banji = new Banji(Integer.parseInt(id), name);
		boolean isSuccess = banjiService.update(banji);
		
		resp.sendRedirect("/JavaWeb1907/banji?method=selectAll");
	}
}
